﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using Common;

namespace webs.website_admin.knowlegebase
{
    public partial class courseeditor : PageBase
    {
        string TitleImage = ConfigurationManager.AppSettings["TitleImage"].ToString();

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Expires = -1;
            PageBegin();

            /// 保存设置
            if (RequestBase.GetFormString("___EVENTTARGET") == "save")
            {
                Save();
            }

            if (!IsPostBack)
            {
                CategoryDataBind();
                if (RequestBase.GetQueryString("articleid") != "")
                    Initialize();
                else
                    txtPublishDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
            }
        }

        #region 下来菜单数据绑定
        void CategoryDataBind()
        {
            DataTable dt = BLL.CommonBLL.Select("KbCategoryBase", "*", " AND DeletionStateCode = 0 ORDER BY LineNum ASC");
            if (dt != null && dt.Rows.Count > 0)
            {
                selCategoryList.Items.Add(new ListItem("\t+\t" + dt.Select("CategoryNumber = '" + RequestBase.GetQueryString("c_number") + "'")[0]["CategoryName"].ToString(), dt.Select("CategoryNumber = '" + RequestBase.GetQueryString("c_number") + "'")[0]["CategoryId"].ToString()));
                DropdownlistDataBind(2, dt.Select("CategoryNumber = '" + RequestBase.GetQueryString("c_number") + "'")[0]["CategoryId"].ToString(), dt);
            }
            else
                DropdownlistDataBind(1, "00000000-0000-0000-0000-000000000000", dt);
        }
        /// <summary>
        /// 类别列表邦定
        /// </summary>
        /// <param name="intLayer">级别</param>
        /// <param name="parentId">父目录ID</param>
        /// <param name="dt">目录数据表</param>
        void DropdownlistDataBind(int intLayer, string parentId, DataTable dt)
        {
            if (dt == null)
                return;

            DataRow[] dr = dt.Select("Layer = " + intLayer.ToString() + " AND ParentId = '" + parentId + "'");

            ArrayList al = new ArrayList();
            ListItem li;

            string str = ""; int intCategoryLayerID = 0;
            foreach (DataRow d in dr)
            {
                str = "　| ";
                intCategoryLayerID = int.Parse(d["Layer"].ToString());
                if (intLayer == 1)
                    str = " + ";
                for (int i = 1; i < intLayer * intLayer; i++)
                    str += "--";

                li = new ListItem("\t" + str + "\t" + d["CategoryName"].ToString(), d["CategoryId"].ToString());
                selCategoryList.Items.Add(li);
                DropdownlistDataBind(intCategoryLayerID + 1, d["CategoryId"].ToString().Trim(), dt);
            }
        }
        #endregion

        #region 数据初始化
        void Initialize()
        {
            Model.KbArticleBase _obj = BLL.KbArticleBase.GetObjById(RequestBase.GetQueryString("articleid"));
            ArticleObj = _obj;
        }

        Model.KbArticleBase ArticleObj
        {
            get
            {
                Model.KbArticleBase _article = new Model.KbArticleBase();

                if (!string.IsNullOrEmpty(RequestBase.GetQueryString("articleid")))
                    _article.ArticleId = RequestBase.GetQueryString("articleid");

                _article.Title = txtTitle.Value.Trim(); 
                _article.CategoryId = BLL.KbCategoryBase.GetCategoryIdByNumber(RequestBase.GetQueryString("c_number"));
                _article.Resume = "";
                _article.Content = txtContent.Text;
                _article.CategoryId = selCategoryList.Value; 
                _article.Resume = txtResume.Value.Trim();

                _article.PublishDate = DateTime.Parse(txtPublishDate.Value.Trim());
                _article.ModifiedOn = DateTime.Now;
                _article.CreatedOn = DateTime.Now;
                _article.ModifiedBy = this.SystemUserId;
                _article.CreatedBy = this.SystemUserId;
                _article.TitlePic = txtUploadControl_TitleImage.Value.Trim().Replace("|", "");

                return _article;
            }
            set
            {
                if (value != null)
                {
                    txtTitle.Value = value.Title.Trim(); 
                    selCategoryList.Value = value.CategoryId;
                    txtResume.Value = value.Resume;
                    txtPublishDate.Value = value.PublishDate.ToString("yyyy-MM-dd");
                    txtContent.Text = value.Content;
                    if (value.TitlePic != "")
                    {
                        txtUploadControl_TitleImage.Value = "|" + value.TitlePic.Trim();
                        divProductImagesList_TitleImage.InnerHtml = "<div class=\"attachmentitems\">" + BLL.AttachmentsBase.GetIcoByExtName(value.TitlePic.Trim().Split('*')[2]) + "&nbsp;&nbsp;<a href='../../" + TitleImage + value.TitlePic.Trim().Split('*')[1] + "' title='Click to open the file' target='_blank'>" + value.TitlePic.Trim().Split('*')[0] + "</a>&nbsp;&nbsp; <a href='javascript:void(0);' filedata='" + value.TitlePic.Trim() + "' itemid='TitleImage' onclick='uploadinit($(this));'>[Delete]</a></div>";
                    }
                }
            }
        }
        #endregion

        #region 保存数据
        void Save()
        {
            string res = "";

            if (RequestBase.GetQueryString("otype").Trim() == "add")
            {
                res = BLL.KbArticleBase.Add(ArticleObj);
            }

            if (RequestBase.GetQueryString("otype").Trim() == "modify")
            {
                res = BLL.KbArticleBase.Update(ArticleObj);
            }

            switch (res.Split('|')[0])
            {
                case "succeeded":
                    JScript.AlertAndReturnValueToParent("信息保存成功!", "window.opener.LoadingData(); window.close();");
                    break;
                default:
                    JScript.Alert(res);
                    break;
            }
        }
        #endregion
    }
}
